package cn.dankal.flyaudio.login;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.utils.TextUtils;

import api.UserServiceFactory;
import cn.dankal.basiclib.pojo.PhoneOrEmailIsExistBean;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.business.Constants;

/**
 * Creater: slientwhale.
 * CreatedTime: 2019/3/2
 * Desc:
 * Remark:
 */
public class AccountPresenter implements AccountCheckContract.Presenter {

    private AccountCheckContract.View view;

    @Override
    public void attachView(@NonNull AccountCheckContract.View view) {
        this.view = view;
    }

    @Override
    public void detachView() {
        this.view = null;
    }

    @Override
    public void checkPhoneOrEmailIsExits(Constants.CheckType checkType, String checkStr, AccountCheckContract.CheckCallback callback) {

        if (TextUtils.isEmpty(checkStr)) {
            callback.onPhoneOrEmailCheckError(checkType.getName()+"不能为空");
            return;
        }

        UserServiceFactory
                .phoneOrEmailIsExist(checkStr, checkType.getStates())
                .subscribe(new AbstractSubscriber<PhoneOrEmailIsExistBean>(view) {

                    @Override
                    public void onNext(PhoneOrEmailIsExistBean phoneOrEmailIsExistBean) {
                        int reason = Integer.valueOf(phoneOrEmailIsExistBean.getReason());
                        switch (reason) {

                            case Constants.PhoneExistStatus.isExist:
                                callback.onPhoneOrEmailCheckIsExist(checkType);
                                break;

                            case Constants.PhoneExistStatus.isNotExist:
                                callback.onPhoneOrEmailCheckIsNotExist(checkType);
                                break;
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                    }
                });
    }


}
